package com.hushow.security.demo.auth.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration

@EnableAuthorizationServer
public class MyAuthorizationServerConfigurerAdapter extends AuthorizationServerConfigurerAdapter {

    private static final Log logger = LogFactory.getLog(MyAuthorizationServerConfigurerAdapter.class);

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private TokenStore tokenStore;

    @Resource
    DataSource dataSource;

    @Resource
    public UserDetailsService myUserDetailsService;

    @Bean
    public TokenStore tokenStore(DataSource dataSource){
        //return new RedisTokenStore(RedisConnectionFactory);
        return new JdbcTokenStore(dataSource);
    }

    /*
     * Redis TokenStore (有Redis场景时使用)
     */
//    @Bean
//    public TokenStore tokenStore(RedisConnectionFactory connectionFactory) {
//        final RedisTokenStore redisTokenStore = new RedisTokenStore(connectionFactory);
//        //prefix
//        redisTokenStore.setPrefix(RESOURCE_ID);
//        return redisTokenStore;
//    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.checkTokenAccess("isAuthenticated()");
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//        clients.inMemory()
//                .withClient("portal-service")
//                .secret("123456")
//                .authorizedGrantTypes("password","authorization_code")
//                .scopes("read")
//                .accessTokenValiditySeconds(3600)
//                .resourceIds("order-service","user-service")
//                .redirectUris("http://www.baidu.com");

        clients.jdbc(dataSource);
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
        endpoints.userDetailsService(myUserDetailsService);
        endpoints.tokenStore(tokenStore);
    }
}
